package mju.cxf.controller.employee;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.entity.Sales;
import mju.cxf.entity.SalesAfter;
import mju.cxf.result.Result;
import mju.cxf.service.SalesAfterService;
import mju.cxf.service.impl.SalesServiceImpl;
import mju.cxf.vo.SalesAfterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 售后相关接口
 */

@RestController
@RequestMapping("/employee/api/safter")
@Api(tags = "售后接口列表")
public class ESalesAfterController {
    @Autowired
    private SalesAfterService salesAfterService;

    @Autowired
    private SalesServiceImpl salesServiceImpl;
    /**
     * 分页查询售后信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    @ApiOperation(value = "分页查询售后信息", notes = "管理员通过此接口分页查询售后信息")
    public Page<SalesAfterVO> getSalesAfterPages(@RequestParam(value = "number",required = false) String number,
                                                 @RequestParam(value = "status", required = false) Integer status, // 添加状态参数
                                                 @RequestParam(value = "current",defaultValue = "1") int current,
                                                 @RequestParam(value = "size",defaultValue = "10") int size) {
        return salesAfterService.selectPage(number,status, current, size);
    }

    /**
     * 查询是否有售后商品
     * @return 分页查询结果
     */
    @GetMapping("/Sales")
    @ApiOperation(value = "查询售后商品")
    public Result<String>  selectSales(@RequestBody SalesAfter salesAfter){
        Sales sales = salesServiceImpl.selectOrderNumber(salesAfter.getOrderNumber());
        Sales newSales =  salesServiceImpl.selectSpecifications(sales.getSpecificationsId());
        if (newSales != null) {
            return Result.success("存在替换商品");
        } else {
            return Result.error("不存在替换商品");
        }
    }
    /**
     * 修改出售商品信息
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "审核售后信息")
    public Result<String> updateSalesAfter(@RequestBody SalesAfter salesAfter) {

        return salesAfterService.updateSalesAfter(salesAfter) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }
}
